
//
//  UrlBuilder.m
//  HelloTaxi
//
//  Created by zhenhua zhou on 12-7-10.
//  Copyright (c) 2012年 ECUST. All rights reserved.
//

#import "UrlBuilder.h"

static UrlBuilder * urlBuilderInstance = nil;

@implementation UrlBuilder


- (UrlBuilder *) init
{
    self = [super init];
    if(self)
    {
        mUrlHeader = @"http://maps.googleapis.com/maps/api/";
        mApiDirection = @"directions/";
        mApiGeoCoding = @"geocode/";
        
        mXmlOutput = @"xml?";
        mJsonOutput = @"json?";
        
        mOrigin = @"origin=";
        mDestination = @"destination=";
        
        mAddress = @"address=";
        
        mSensor = @"sensor=false";
    }
    
return self;
}

- (void) dealloc
{
    if(mUrlHeader)
    {
        [mUrlHeader dealloc];
    }
    if(mApiDirection)
    {
        [mApiDirection dealloc];
    }
    if(mApiGeoCoding)
    {
        [mApiGeoCoding dealloc];
    }
    if(mXmlOutput)
    {
        [mXmlOutput dealloc];
    }
    if(mJsonOutput)
    {
        [mJsonOutput dealloc];
    }
    if(mOrigin)
    {
        [mOrigin dealloc];
    }
    if(mDestination)
    {
        [mDestination dealloc];
    }
    if(mAddress)
    {
        [mAddress dealloc];
    }
    if(mSensor)
    {
        [mSensor dealloc];
    }

    
    
    [super dealloc];
}

- (NSString *) buildDirectionUrlByAddressFrom:(NSString *)origin to:(NSString *) destination
{
    NSMutableString * temp = [[NSMutableString alloc] initWithString:mUrlHeader];
    [temp appendString:mApiDirection];
    //only support json now
    [temp appendString:mJsonOutput];
    
    [temp appendString:mOrigin];
    [temp appendString:origin];
    [temp appendString:@"&"];
    [temp appendString:mDestination];
    [temp appendString:destination];
    [temp appendString:@"&"];
    [temp appendString:mSensor];
    
    NSString * result = [[[NSString alloc] initWithString:temp] autorelease];
    
    [temp dealloc];
    return result;
}

- (NSString *) buildDirectionUrlByPosFrom:(GeoPos *)origin to:(GeoPos *) destination
{
    NSMutableString * temp = [[[NSMutableString alloc] initWithString:mUrlHeader] autorelease];
    [temp appendString:mApiDirection];
    //only support json now
    [temp appendString:mJsonOutput];
    
    [temp appendString:mOrigin];
    [temp appendString:[NSString stringWithFormat:@"%d",origin.Lat]];
    [temp appendString:@","];
    [temp appendString:[NSString stringWithFormat:@"%d",origin.Lon]];
    [temp appendString:@"&"];
    [temp appendString:mDestination];
    [temp appendString:[NSString stringWithFormat:@"%d",destination.Lat]];
    [temp appendString:@","];
    [temp appendString:[NSString stringWithFormat:@"%d",destination.Lon]];
    [temp appendString:@"&"];
    [temp appendString:mSensor];
    
   // NSString * result = [[[NSString alloc] initWithString:temp] autorelease];
    
   // [temp dealloc];
    return [temp retain];
}

- (NSString *) buildDirectionUrlByFromPos:(GeoPos *)origin toAddress:(NSString *) destination
{
    NSMutableString * temp = [[[NSMutableString alloc] initWithString:mUrlHeader] autorelease];
    [temp appendString:mApiDirection];
    //only support json now
    [temp appendString:mJsonOutput];
    
    [temp appendString:mOrigin];
    [temp appendString:[NSString stringWithFormat:@"%lf",origin.Lat]];
    [temp appendString:@","];
    [temp appendString:[NSString stringWithFormat:@"%lf",origin.Lon]];
    [temp appendString:@"&"];
    [temp appendString:mDestination];
    [temp appendString:destination];
    [temp appendString:@"&"];
    [temp appendString:mSensor];
    
    // NSString * result = [[[NSString alloc] initWithString:temp] autorelease];
    
    // [temp dealloc];
    return [temp retain];
}

- (NSString *) buildGeoCodingUrl:(NSString *)address
{
    NSMutableString * temp = [[[NSMutableString alloc] initWithString:mUrlHeader] autorelease];
    [temp appendString:mApiDirection];
    //only support json now
    [temp appendString:mJsonOutput];
    
    [temp appendString:mAddress];
    [temp appendString:address];
    [temp appendString:@"&"];
    [temp appendString:mSensor];
    
  //  NSString * result = [[[NSString alloc] initWithString:temp] autorelease];
    
  //  [temp dealloc];
    return [temp retain];
}

+ (UrlBuilder *) getInstance
{
    if(urlBuilderInstance==nil)
    {
        urlBuilderInstance =[[UrlBuilder alloc] init];
    }
    return urlBuilderInstance;
}

@end
